package com.zklh.book.system.view.render;

import com.zklh.book.system.common.enums.StatusEnum;
import com.zklh.book.system.model.Book;
import com.zklh.book.system.service.BookService;
import com.zklh.book.system.service.impl.BookServiceImpl;
import com.zklh.book.system.view.ViewRenderer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyBookTableViewRenderer implements ViewRenderer {
    /**
     * 需要参数 books List<Book>
     * @param params
     */
    @Override
    public void render(Map<String, Object> params) {
        List<Book> books = (List<Book>) params.get("books");
        String format = "%-8s\t%-8s";
        System.out.printf(format,"--------","--------");
        System.out.println();
        // 打印表格头部信息
        System.out.printf(format, "编号", "书名");
        System.out.println();
        StringBuilder sb = new StringBuilder();
        for (Book book : books) {

            String code = book.getCode();
            String name = book.getName();
            if(code.length() > 5) {
                name = code.substring(0,5) +"..";
            }
            if(name.length() > 5) {
                name = name.substring(0,5) +"..";
            }
            System.out.printf(format, code, name);
            System.out.println();
        }
        System.out.printf(format,"--------","--------");
        System.out.println();
    }


    //这段是不是测试没有实际用途?
    public static void main(String[] args) {
        BookTableViewRenderer render = new BookTableViewRenderer();
        BookService bookService = new BookServiceImpl();
        Map<String,Object> params = new HashMap<>();
        params.put("books", bookService.getAll());
        render.render(params);
    }
}
